package com.ivan.itoutiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.itoutiao.entity.Channel;
import com.ivan.itoutiao.service.ChannelService;
import com.ivan.itoutiao.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ivan
 * @date 2021/1/9 22:36
 * @Description: 频道controller
 */
@Api("频道controller")
@RestController
@RequestMapping("/channel")
@CrossOrigin
public class ChannelController {

    @Autowired
    ChannelService channelService;

    @ApiOperation(value = "查询所有频道信息")
    @GetMapping("getChannels")
    public CommonResult getChannels() {

        List<Channel> list = channelService.list(null);

        if (list!=null) {
            return CommonResult.success().data(list);
        }
        return CommonResult.fail().message("未查询到频道信息！");
    }

    @ApiOperation(value = "分页查询频道列表信息")
    @GetMapping("getArticlesByPage")
    public CommonResult getArticlesByPage(@RequestParam("page")Long page,
                                    @RequestParam("pageSize")Long pageSize,
                                    @RequestParam(value = "channelName", required = false)String channelName,
                                    @RequestParam(value = "beginDate", required = false)String beginDate,
                                    @RequestParam(value = "endDate",required = false)String endDate) {

        //创建分页查询对象
        Page<Channel> pageChannel = new Page<>(page,pageSize);

        //构建查询条件
        QueryWrapper<Channel> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(channelName)) {
            wrapper.eq("name",channelName);
        }
        if(!StringUtils.isEmpty(beginDate)) {
            wrapper.ge("update_date",beginDate + " 00:00:00");
        }
        if(!StringUtils.isEmpty(endDate)) {
            wrapper.le("update_date",endDate + " 23:59:59");
        }
        //按修改时间倒序排序
        wrapper.orderByDesc("create_date");

        channelService.page(pageChannel, wrapper);

        //处理封面图片
        List<Channel> records = pageChannel.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("totalCount", pageChannel.getTotal());
        data.put("results", records);

        return CommonResult.success().data(data);
    }

    @ApiOperation(value = "根据id删除频道")
    @DeleteMapping("deleteChannel/{channelId}")
    public CommonResult deleteArticle(@PathVariable("channelId")String channelId) {
        boolean remove = channelService.removeById(channelId);
        if (remove) {
            return CommonResult.success().message("删除成功！");
        }
        return CommonResult.fail().message("删除失败！");
    }

}
